<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>layui表格</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
</head>
<body>
<form class="layui-form" lay-filter="formtest" id="dataform"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
    <div class="layui-form-item">
        <label class="layui-form-label">输入框</label>
        <div class="layui-input-block">
            <input type="text" name="inputS" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">下拉选择框</label>
        <div class="layui-input-block">
            <select name="interest" lay-filter="aihao">
                <option value="0">写作</option>
                <option value="1">阅读</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">复选框</label>
        <div class="layui-input-block">
            <input type="checkbox" name="like[write]" title="写作" lay-filter="write">
            <input type="checkbox" name="like[read]" title="阅读">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">开关关</label>
        <div class="layui-input-block" >
            <input type="checkbox" name="switch1" lay-skin="switch" lay-filter="swit">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">开关开</label>
        <div class="layui-input-block">
            <input type="checkbox" checked lay-skin="switch">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">单选框</label>
        <div class="layui-input-block">
            <!--两个赋一样的值 lay-filter="nanradio" 这样就监控那两个了-->
            <input type="radio" name="sex" value="0" title="男" lay-filter="nanradio">
            <input type="radio" name="sex" value="1" title="女" checked lay-filter="nanradio">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">请填写描述</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入内容" name="txtArea" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="*">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
    <!-- 更多表单结构排版请移步文档左侧【页面元素-表单】一项阅览 -->
</form>
<script src="/layui/layui.js"></script>
<script src="/js/index.js"></script>
<script src="/js/jquery.min.js"></script>
<script>
    layui.use('form', function () {
        var form = layui.form;
        form.on('select(aihao)', function (data) {
            console.log(data.value);
        });
        form.on('checkbox(write)', function(data){
            console.log(data.elem); //得到checkbox原始DOM对象
            console.log(data.elem.checked); //是否被选中，true或者false
            console.log(data.value); //复选框value值，也可以通过data.elem.value得到
            console.log(data.othis); //得到美化后的DOM对象
        });
        form.on('switch(swit)', function(data){
            console.log(data.elem); //得到checkbox原始DOM对象
            console.log(data.elem.checked); //开关是否开启，true或者false
            console.log(data.value); //开关value值，也可以通过data.elem.value得到
            console.log(data.othis); //得到美化后的DOM对象
        });
        form.on('radio(nanradio)', function(data){
            console.log(data.value); //被点击的radio的value值
        });
        form.on('submit(*)', function(data){
            //console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
            //console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            var index = layer.msg('提交中，请稍后',{icon:16,time:false,shade:0.8});
            setTimeout(function () {
                layer.close(index);
                $.ajax({
                    url:'userAdmin',
                    data:$("#dataform").serialize(),
                    dataType:"json",
                    cache:false,
                    success:function (data) {
                        if (data.code==250){
                            layer.msg("提交失败!");
                        } else if(data.code==200){
                            layer.msg("提交成功!");
                        }
                    }
                })
            })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
    });

</script>
</body>
</html>